﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MixNP.Web.Classifieds.Account
{
    public partial class SignUp : MixNP.BusinessLayer.BaseClass
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SignUpButton_Click(object sender, EventArgs e)
        {
            string name = NameTextBox.Text;
            string nickName = NickNameTextBox.Text;
            string emailAddress = EmailAddressTextBox.Text;
            string password = PasswordTextBox.Text;
            string salt = Guid.NewGuid().ToString();

            string hashedPassword = string.Empty;

            if (!ConfirmPasswordTextBox.Text.Equals(PasswordTextBox.Text))
            {
                ErrorLiteral.Text = "Passwords do not match.";
            }

            if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(nickName) || string.IsNullOrWhiteSpace(emailAddress) || string.IsNullOrWhiteSpace(password))
            {
                ErrorLiteral.Text = "Invalid form data.";
            }



            hashedPassword = Pes.Utility.Conversion.HashSha512(password, salt);
            if (MixNP.BusinessLayer.Users.CreateUser(emailAddress, name, nickName, salt, hashedPassword))
            {
                if (MixNP.BusinessLayer.Email.SendConfirmationEmail(this.Page, emailAddress))
                {
                    MixNP.BusinessLayer.Email.ProcessEmails();
                    Response.Redirect("~/sign-up/confirm/email-sent.mix");
                }
            }
        }
    }
}